import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class MemberCashOutService extends BaseService<any> {
  private readonly logger = new Logger(MemberCashOutService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: MemberCashOutService_api::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $where['member_id'] = $this->member_id;         $where['site_id'] = $this->site_id;         $field = 'id,site_id,cash_out_no,member_id,account_type,tr...
where.member_id = this.member_id;
        where.site_id = this.site_id;
        const field = 'id,site_id,cash_out_no,member_id,account_type,transfer_type,transfer_realname,transfer_mobile,transfer_bank,transfer_account,transfer_status,transfer_time,apply_money,rate,service_money,money,audit_time,status,remark,create_time,refuse_reason';
        const search_model = this.model.where(where).withSearch(['member_id','status', 'create_time'),where).with(['memberInfo', 'transfer')).field(field).append(['account_type_name', 'transfer_type_name', 'status_name', 'transfer_status_name')).order('create_time desc'];

        return this.pageQuery(search_model);
    }

    /**
     * 提现详情
     * @param int id
     * @return */
    async getInfo(int id)
    {
        const field = 'id,site_id,cash_out_no,member_id,transfer_type,transfer_realname,transfer_mobile,transfer_bank,transfer_account,transfer_fail_reason,transfer_time,apply_money,rate,service_money,money,audit_time,status,remark,create_time,refuse_reason, transfer_no, transfer_payee, transfer_payment_code';
        return this.model.where([['id', '=', id], ['site_id', '=', this.site_id], ['member_id', '=', this.member_id))).with(['memberInfo', 'transfer')).field(field).append(['account_type_name', 'transfer_type_name', 'status_name', 'transfer_status_name')).findOrEmpty().toArray(];
    }


    /**
     * 申请提现
     * @param data
     * @return true
     */
    async apply(data){

        return this.coreMemberCashOutService.apply(this.site_id, this.member_id, data);
    }


    /**
     * 撤销提现申请
     * @param int id
     * @return true
     */
    async cancel(int id){
        const cash_out = this.model.where([
            ['site_id', '=', this.site_id],
            ['id', '=', id],
            ['member_id', '=', this.member_id),
        )).findOrEmpty(];

        if(cash_out.isEmpty()) throw new ApiBusinessException('RECHARGE_LOG_NOT_EXIST');
//        if(cash_out.status != MemberCashOutDict.WAIT_AUDIT) throw new BusinessException('CASHOUT_STATUS_NOT_IN_WAIT_AUDIT'];
        this.coreMemberCashOutService.cancel(this.site_id, id);
//        cash_out.save(
//
//            [
//                cancel_time: time(),
//                status: MemberCashOutDict.CANCEL
//            ]
//        ];
//        this.coreMemberCashOutService.giveback(this.site_id, cash_out);
        return true;
    }

    /**
     * 获取提现配置
     * @return */
    async getCashOutConfig(){
        return this.coreMemberConfigService.getCashOutConfig(this.site_id);
    }

    /**
     * 提现转账(主要用于微信商家转账)
     * @param int id
     * @param data
     * @return true
     */
    async transfer(int id, data){
        data.channel = this.channel;
        const result = this.coreMemberCashOutService.transfer(this.site_id, id, data);
        return result;
    }
}
  }

  /**
   * getInfo
   * 对应 PHP: MemberCashOutService_api::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'id,site_id,cash_out_no,member_id,transfer_type,transfer_realname,transfer_mobile,transfer_bank,transfer_account,transfer_fail_reason,transfe...
const field = 'id,site_id,cash_out_no,member_id,transfer_type,transfer_realname,transfer_mobile,transfer_bank,transfer_account,transfer_fail_reason,transfer_time,apply_money,rate,service_money,money,audit_time,status,remark,create_time,refuse_reason, transfer_no, transfer_payee, transfer_payment_code';
        return this.model.where([['id', '=', id], ['site_id', '=', this.site_id], ['member_id', '=', this.member_id))).with(['memberInfo', 'transfer')).field(field).append(['account_type_name', 'transfer_type_name', 'status_name', 'transfer_status_name')).findOrEmpty().toArray(];
    }


    /**
     * 申请提现
     * @param data
     * @return true
     */
    async apply(data){

        return this.coreMemberCashOutService.apply(this.site_id, this.member_id, data);
    }


    /**
     * 撤销提现申请
     * @param int id
     * @return true
     */
    async cancel(int id){
        const cash_out = this.model.where([
            ['site_id', '=', this.site_id],
            ['id', '=', id],
            ['member_id', '=', this.member_id),
        )).findOrEmpty(];

        if(cash_out.isEmpty()) throw new ApiBusinessException('RECHARGE_LOG_NOT_EXIST');
//        if(cash_out.status != MemberCashOutDict.WAIT_AUDIT) throw new BusinessException('CASHOUT_STATUS_NOT_IN_WAIT_AUDIT'];
        this.coreMemberCashOutService.cancel(this.site_id, id);
//        cash_out.save(
//
//            [
//                cancel_time: time(),
//                status: MemberCashOutDict.CANCEL
//            ]
//        ];
//        this.coreMemberCashOutService.giveback(this.site_id, cash_out);
        return true;
    }

    /**
     * 获取提现配置
     * @return */
    async getCashOutConfig(){
        return this.coreMemberConfigService.getCashOutConfig(this.site_id);
    }

    /**
     * 提现转账(主要用于微信商家转账)
     * @param int id
     * @param data
     * @return true
     */
    async transfer(int id, data){
        data.channel = this.channel;
        const result = this.coreMemberCashOutService.transfer(this.site_id, id, data);
        return result;
    }
}
  }

  /**
   * apply
   * 对应 PHP: MemberCashOutService_api::apply()
   * 逻辑类型: undefined - undefined
   */
  async apply(data: any[]) {
    // 基于PHP真实逻辑: apply
    // PHP原文: return (new CoreMemberCashOutService())->apply($this->site_id, $this->member_id, $data);     }       /**      * 撤销提现申请      * @param int $id      * @r...
return this.coreMemberCashOutService.apply(this.site_id, this.member_id, data);
    }


    /**
     * 撤销提现申请
     * @param int id
     * @return true
     */
    async cancel(int id){
        const cash_out = this.model.where([
            ['site_id', '=', this.site_id],
            ['id', '=', id],
            ['member_id', '=', this.member_id),
        )).findOrEmpty(];

        if(cash_out.isEmpty()) throw new ApiBusinessException('RECHARGE_LOG_NOT_EXIST');
//        if(cash_out.status != MemberCashOutDict.WAIT_AUDIT) throw new BusinessException('CASHOUT_STATUS_NOT_IN_WAIT_AUDIT'];
        this.coreMemberCashOutService.cancel(this.site_id, id);
//        cash_out.save(
//
//            [
//                cancel_time: time(),
//                status: MemberCashOutDict.CANCEL
//            ]
//        ];
//        this.coreMemberCashOutService.giveback(this.site_id, cash_out);
        return true;
    }

    /**
     * 获取提现配置
     * @return */
    async getCashOutConfig(){
        return this.coreMemberConfigService.getCashOutConfig(this.site_id);
    }

    /**
     * 提现转账(主要用于微信商家转账)
     * @param int id
     * @param data
     * @return true
     */
    async transfer(int id, data){
        data.channel = this.channel;
        const result = this.coreMemberCashOutService.transfer(this.site_id, id, data);
        return result;
    }
}
  }

  /**
   * cancel
   * 对应 PHP: MemberCashOutService_api::cancel()
   * 逻辑类型: undefined - undefined
   */
  async cancel(id: number) {
    // 基于PHP真实逻辑: cancel
    // PHP原文: $cash_out = $this->model->where([             ['site_id', '=', $this->site_id],             ['id', '=', $id],             ['member_id', '=', $this->me...
const cash_out = this.model.where([
            ['site_id', '=', this.site_id],
            ['id', '=', id],
            ['member_id', '=', this.member_id),
        )).findOrEmpty(];

        if(cash_out.isEmpty()) throw new ApiBusinessException('RECHARGE_LOG_NOT_EXIST');
//        if(cash_out.status != MemberCashOutDict.WAIT_AUDIT) throw new BusinessException('CASHOUT_STATUS_NOT_IN_WAIT_AUDIT'];
        this.coreMemberCashOutService.cancel(this.site_id, id);
//        cash_out.save(
//
//            [
//                cancel_time: time(),
//                status: MemberCashOutDict.CANCEL
//            ]
//        ];
//        this.coreMemberCashOutService.giveback(this.site_id, cash_out);
        return true;
    }

    /**
     * 获取提现配置
     * @return */
    async getCashOutConfig(){
        return this.coreMemberConfigService.getCashOutConfig(this.site_id);
    }

    /**
     * 提现转账(主要用于微信商家转账)
     * @param int id
     * @param data
     * @return true
     */
    async transfer(int id, data){
        data.channel = this.channel;
        const result = this.coreMemberCashOutService.transfer(this.site_id, id, data);
        return result;
    }
}
  }

  /**
   * getCashOutConfig
   * 对应 PHP: MemberCashOutService_api::getCashOutConfig()
   * 逻辑类型: undefined - undefined
   */
  async getCashOutConfig() {
    // 基于PHP真实逻辑: getCashOutConfig
    // PHP原文: return (new CoreMemberConfigService())->getCashOutConfig($this->site_id);     }      /**      * 提现转账(主要用于微信商家转账)      * @param int $id      * @param a...
return this.coreMemberConfigService.getCashOutConfig(this.site_id);
    }

    /**
     * 提现转账(主要用于微信商家转账)
     * @param int id
     * @param data
     * @return true
     */
    async transfer(int id, data){
        data.channel = this.channel;
        const result = this.coreMemberCashOutService.transfer(this.site_id, id, data);
        return result;
    }
}
  }

  /**
   * transfer
   * 对应 PHP: MemberCashOutService_api::transfer()
   * 逻辑类型: undefined - undefined
   */
  async transfer(id: any[], data: any[]) {
    // 基于PHP真实逻辑: transfer
    // PHP原文: $data['channel'] = $this->channel;         $result = (new CoreMemberCashOutService())->transfer($this->site_id, $id, $data);         return $result;  ...
data.channel = this.channel;
        const result = this.coreMemberCashOutService.transfer(this.site_id, id, data);
        return result;
    }
}
  }
}
